"""
状态栏
"""
import json
# import os
from tools import *
from tkinter import messagebox
from sys_config import *

class SysStatusBar():
    def __init__(self):
        self.menufile = 'menu.json'
        self.status_bar_height = 25
        self.tools    = Tools()
        self.config   = SysConfig()
        self.config.init()

    def gen_status_bar(self, win, sect_width, click_cb = False, dbl_click_cb = False):
        """生成状态栏:父窗口、宽度描述字符串（字符宽度,逗号分隔）、单击事件、双击事件
        """
        # screen_width = win.winfo_width()
        # print("gen_status_bar", screen_width)

        frame = Frame(win)
        frame.place(anchor='w', x=0, y=500, width=1000, height=self.status_bar_height, bordermode='inside')

        font_size = self.config.value('ui', 'font_size', 12)
        self.status_bar_lbl = []
        self.status_bar_text = []
        total_width = 0
        width_list = sect_width.split(',')
        i = 0
        for item in width_list:
            s = StringVar()
            s.set('txt-' + str(i))
            self.status_bar_text.append(s)
            
            if i == len(width_list) - 1:
                # if area_width - total_width > 0: w = area_width - total_width #最右一格占满
                self.right_lbl_x = total_width  # 最右一格的x
                w = 2000
            else:
                w = int(item)

            total_width += w   
            lbl  = Label(frame, 
                         width=w, 
                         height=1, 
                         anchor='w', 
                         relief='sunken', 
                         borderwidth=2, 
                         font=('宋体', font_size),
                         textvariable = self.status_bar_text[i]
                         )
            
            if click_cb:     lbl.bind("<Button-1>", lambda event, index=i: click_cb(index))  # 鼠标左键点击事件
            if dbl_click_cb: lbl.bind("<Double-1>", lambda event, index=i: dbl_click_cb(index))  # 鼠标左键点击事件
            # if i == len(width_list) - 1:
            #     lbl.pack(side='right', fill='x')
            # else:
            lbl.pack(side='left')

            self.status_bar_lbl.append(lbl)
            i += 1

        self.status_bar_frame = frame
        return frame
    
    def adjust(self, win_width, win_height):
        """调整状态栏的位置
        """
        # print(f"{__name__}:{win_width},{win_height},{self.right_lbl_x}")
        self.status_bar_frame.place(anchor='w', 
                                    x = 0, 
                                    y = win_height - self.status_bar_height + 11, 
                                    width = win_width, 
                                    height=self.status_bar_height)

    def set_text(self, index, text):
        """设置状态栏的文字:序号、文字
        """
        print(f"{__name__}:{index},{text}")
        if index < 0 or index > len(self.status_bar_text) - 1: return

        self.status_bar_text[index].set(text)

    def set_all_text(self, text_list):
        """设置状态栏中所有子项的文字:文字的列表
        """
        print(f"{__name__}")
        for i in range(len(text_list)):
            if i >= len(self.status_bar_text): return
            self.status_bar_text[i].set(text_list[i])
